//
//  AccountViewController.m
//  JianChaRiBao
//
//  Created by Changxy on 13-5-31.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "AccountViewController.h"
#import "AuthorManager.h"
#import "AccountMemageViewController.h"
#import "AppDelegate.h"
#import "RegisterViewController.h"
@interface AccountViewController ()

@end

@implementation AccountViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess:) name:kLoginSuccess object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logoinFail:) name:kLoginFail object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationLogoOut:) name:kLogout object:nil];
    }
    return self;
}
-(void)loginSuccess:(NSNotification *)notification{
    [self removeloadingView];
    logoOut.hidden = NO;
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"登录成功" ]autorelease];
    [promptView show];

    userView.hidden = YES;
    logoinView.hidden = NO;
    accountButton2.hidden = NO;
    
    usernameLabel.text = [[AuthorManager defaultManager].userInfo objectForKey:@"user_name"];
    emailLabel.text = [[AuthorManager defaultManager].userInfo objectForKey:@"email"];
    logoinBtn.hidden = YES;
    registerBtn.hidden = YES;
    [UIView animateWithDuration:0.3 animations:^{
        desView.frame = CGRectMake(54, 140, 260, 177);
    }];
    
}
-(void)logoinFail:(NSNotification *)notification{
    [self removeloadingView];
    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"登录失败" ]autorelease];
    [promptView show];
}
-(void)notificationLogoOut:(NSNotification *)notification{
    logoOut.hidden = YES;
    userView.hidden = NO;
    logoinView.hidden = YES;
    passwordText.text = @"";
    accountButton2.hidden = YES;
    [UIView animateWithDuration:0.3 animations:^{
        logoinBtn.hidden = NO;
//        registerBtn.hidden = NO;
        desView.frame = CGRectMake(54, 194, 260, 177);
    }];

    ABPromptView *promptView = [[[ABPromptView alloc] initWithTitle:@"注销成功" ]autorelease];
    [promptView show];

}
//登录按钮
- (IBAction)logoin:(id)sender {
    [usernameText resignFirstResponder];
    [passwordText resignFirstResponder];

    if (usernameText.text == nil || [usernameText.text isEqualToString:@""] || passwordText.text == nil || [passwordText.text isEqualToString:@""] ) {
        ABPromptView * abp = [[[ABPromptView alloc]initWithTitle:@"非法用户名或密码"]autorelease];
        [abp show];
        return;
    }
    [self showLoadingView];
    [[AuthorManager defaultManager] loginWithUsername:usernameText.text password:passwordText.text];
}
//注册按钮
- (IBAction)registerClicked:(id)sender {
    RegisterViewController * registerView = [[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
    [[UIApplication rootNavController] pushViewController:registerView animated:YES];
    [registerView release];
}

//账户管理
- (IBAction)account:(id)sender {
    if ([[AuthorManager defaultManager] hasLoggedIn] == NO) {
        ABPromptView * abp = [[[ABPromptView alloc]initWithTitle:@"请登录"]autorelease];
        [abp show];
        return;
    }else{
        AccountMemageViewController * acc = [[AccountMemageViewController alloc]initWithNibName:@"AccountMemageViewController" bundle:nil];
        [[UIApplication rootNavController]pushViewController:acc animated:YES];
        [acc release];
    }
}
//充值
- (IBAction)voucher:(id)sender {
}
//更新
- (IBAction)upData:(id)sender {
}
//激活中邮卡
- (IBAction)activate:(id)sender {
}
//注销
- (IBAction)logoOut:(id)sender {
    [[AuthorManager defaultManager]logOut];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{

    
}
-(void)textFieldDidEndEditing:(UITextField *)textField{

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if ([[AuthorManager defaultManager]hasLoggedIn]) {
        logoOut.hidden = NO;
    }else{
        logoOut.hidden = YES;
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [usernameText resignFirstResponder];
    [passwordText resignFirstResponder];
}

- (void)showLoadingView
{
    if (loadingView.superview) {
        return;
    }
    if (loadingView == nil) {
        loadingView = [[[ABSpringLoadingView alloc] initWithFrame:self.view.bounds] autorelease];
    }
    loadingView.alpha = 0;
    [self.view addSubview:loadingView];
    [UIView animateWithDuration:0.3 animations:^{
        loadingView.alpha = 1;
    }];
}

- (void)removeloadingView
{
    if (loadingView) {
        [UIView animateWithDuration:0.3 animations:^{
            loadingView.alpha = 0;
        } completion:^(BOOL finished) {
            [loadingView removeFromSuperview];
            loadingView = nil;
        }];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [usernameText release];
    [passwordText release];
    [logoinBtn release];
    [registerBtn release];
    [accountButton release];
    [voucherButton release];
    [upDataButton release];
    [ActivateButton release];
    [userView release];
    [desView release];
    [logoinView release];
    [usernameLabel release];
    [emailLabel release];
    [logoOut release];
    [accountButton2 release];
    [super dealloc];
}
- (void)viewDidUnload {
    [usernameText release];
    usernameText = nil;
    [passwordText release];
    passwordText = nil;
    [logoinBtn release];
    logoinBtn = nil;
    [registerBtn release];
    registerBtn = nil;
    [accountButton release];
    accountButton = nil;
    [voucherButton release];
    voucherButton = nil;
    [upDataButton release];
    upDataButton = nil;
    [ActivateButton release];
    ActivateButton = nil;
    [userView release];
    userView = nil;
    [desView release];
    desView = nil;
    [logoinView release];
    logoinView = nil;
    [usernameLabel release];
    usernameLabel = nil;
    [emailLabel release];
    emailLabel = nil;
    [logoOut release];
    logoOut = nil;
    [accountButton2 release];
    accountButton2 = nil;
    [super viewDidUnload];
}
@end
